#include "myCalServer.hpp"
#include <cstdlib>
#include <memory>

Response calculator(const Request &rq)
{
    int x = rq._x;
    char op = rq._op;
    //cout << "_op:" << op << endl;
    int y = rq._y;
    int result = 0;
    int code = 0;
    Response rsp;
    switch (op)
    {
    case '+':
        result = x + y;
        code = 1;
        break;
    case '-':
        result = x - y;
        code = 1;
        break;
    case '*':
        result = x * y;
        code = 1;
        break;
    case '/':
        if (0 == y)
        {
            code = 0;
        }
        else
        {
            result = x / y;
            code = 1;
        }
        break;
    case '%':
        if (0 == y)
        {
            code = 0;
        }
        else
        {
            result = x % y;
            code = 1;
        }
        break;
    default:
        logMessge(UNKONW, "超出计算功能");
        break;
    }
    Response resp(result,code);
    return resp;
}

int main(int arc, char *args[])
{
    uint16_t port = toInt(args[1]);
    unique_ptr<tcpServer> tsvr(new tcpServer(port, calculator));

    tsvr->Init();
    tsvr->Start();
    return 0;
}